package com.ruoyi.lowcode.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.lowcode.domain.bo.LcDirectoryMenuBo;
import com.ruoyi.lowcode.domain.vo.LcDirectoryMenuVo;
import com.ruoyi.lowcode.domain.LcDirectoryMenu;
import com.ruoyi.lowcode.mapper.LcDirectoryMenuMapper;
import com.ruoyi.lowcode.service.ILcDirectoryMenuService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 目录菜单Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-04
 */
@RequiredArgsConstructor
@Service
public class LcDirectoryMenuServiceImpl implements ILcDirectoryMenuService {

    private final LcDirectoryMenuMapper baseMapper;

    /**
     * 查询目录菜单
     */
    @Override
    public LcDirectoryMenuVo queryById(Long menuId){
        return baseMapper.selectVoById(menuId);
    }

    /**
     * 查询目录菜单列表
     */
    @Override
    public TableDataInfo<LcDirectoryMenuVo> queryPageList(LcDirectoryMenuBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<LcDirectoryMenu> lqw = buildQueryWrapper(bo);
        Page<LcDirectoryMenuVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询目录菜单列表
     */
    @Override
    public List<LcDirectoryMenuVo> queryList(LcDirectoryMenuBo bo) {
        LambdaQueryWrapper<LcDirectoryMenu> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    @Override
    public List<LcDirectoryMenu> selectDirectoryMenu(LcDirectoryMenu directoryMenu) {
        LambdaQueryWrapper<LcDirectoryMenu> lqw = new LambdaQueryWrapper<>();
        lqw.eq(LcDirectoryMenu::getDelFlag, "0")
            .like(StringUtils.isNotBlank(directoryMenu.getMenuName()), LcDirectoryMenu::getMenuName, directoryMenu.getMenuName())
            .eq(StringUtils.isNotBlank(directoryMenu.getStatus()), LcDirectoryMenu::getStatus, directoryMenu.getStatus())
            .eq(StringUtils.isNotBlank(directoryMenu.getType()), LcDirectoryMenu::getType, directoryMenu.getType())
            .orderByAsc(LcDirectoryMenu::getParentId)
            .orderByAsc(LcDirectoryMenu::getOrderNum);
        return baseMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<LcDirectoryMenu> buildQueryWrapper(LcDirectoryMenuBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<LcDirectoryMenu> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getParentId() != null, LcDirectoryMenu::getParentId, bo.getParentId());
        lqw.eq(StringUtils.isNotBlank(bo.getAncestors()), LcDirectoryMenu::getAncestors, bo.getAncestors());
        lqw.like(StringUtils.isNotBlank(bo.getMenuName()), LcDirectoryMenu::getMenuName, bo.getMenuName());
        lqw.eq(bo.getOrderNum() != null, LcDirectoryMenu::getOrderNum, bo.getOrderNum());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), LcDirectoryMenu::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增目录菜单
     */
    @Override
    public Boolean insertByBo(LcDirectoryMenuBo bo) {
        LcDirectoryMenu add = BeanUtil.toBean(bo, LcDirectoryMenu.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setMenuId(add.getMenuId());
        }
        return flag;
    }

    /**
     * 修改目录菜单
     */
    @Override
    public Boolean updateByBo(LcDirectoryMenuBo bo) {
        LcDirectoryMenu update = BeanUtil.toBean(bo, LcDirectoryMenu.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(LcDirectoryMenu entity){
        //TODO 做一些数据校验,如唯一约束
        LcDirectoryMenu parentDirectoryMenu = baseMapper.selectById(entity.getParentId());
        // 如果父节点不为正常状态,则不允许新增子节点
        if (!UserConstants.DEPT_NORMAL.equals(parentDirectoryMenu.getStatus())) {
            throw new ServiceException("菜单停用，不允许新增");
        }
        entity.setAncestors(parentDirectoryMenu.getAncestors() + StringUtils.SEPARATOR + entity.getParentId());
    }

    /**
     * 批量删除目录菜单
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
